/*****************************************************************************
 * 74hc148.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "hardware/encoder/encoder.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "74hc148"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
static dev_encoder_pin_t *encoder_pin;
/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

int dev_encoder_init(dev_encoder_pin_t pin[], int cnt)
{
    int rc = 0;

    plum_hal_gpio_cof_t cof = {
        .mode       = PLUM_HAL_GPIO_MODE_COMMON,
        .config.com = PLUM_HAL_GPIO_MODE_INPUT,
    };
    plum_u8 loop;
    for (loop = 0; loop < cnt; loop++) {
        plum_hal_gpio_init(pin->pin_a0, &cof);
        plum_hal_gpio_init(pin->pin_a1, &cof);
        plum_hal_gpio_init(pin->pin_a2, &cof);
    }

    encoder_pin = pin;

    return (rc);
}

int dev_encoder_read_grp(int idx)
{
    int value = 0;

    plum_bit a0 = plum_hal_gpio_read(encoder_pin[idx].pin_a0);
    LOG_D("read pin[0x%08X] value : %d", encoder_pin[idx].pin_a0, a0);

    plum_bit a1 = plum_hal_gpio_read(encoder_pin[idx].pin_a1);
    LOG_D("read pin[0x%08X] value : %d", encoder_pin[idx].pin_a1, a1);

    plum_bit a2 = plum_hal_gpio_read(encoder_pin[idx].pin_a2);
    LOG_D("read pin[0x%08X] value : %d", encoder_pin[idx].pin_a2, a2);

    value = a0 | (a1 << 1) | (a2 << 2);
    LOG_I("read encoder[%d] value : %d(0x%02X)", idx, value, value);

    return (value);
}

bool dev_encode_read_idx(int grp, int idx)
{
    int value = dev_encoder_read(grp);
    LOG_D("read coder[%d] value : %02X", grp, value);

    bool sta = false;
    switch (value) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        default:
            break;
    }

    return (sta);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
